<?php
/* Dsql project v.0.2.1
 * created by aimodify ~ 2011
 */
class Dsql {

    protected $link;
    protected $sql;

    private function generateError($errorText) {
        print ($errorText ? "Dsql Error: " . $errorText : 'mysql Error: ' . mysql_error($this->link));
        die;
    }

    public function __construct($host, $name, $pass) {

        $this->link = mysql_connect($host, $name, $pass);
        if (!$this->link) {
            die('Could not connect: ' . mysql_error());
        }
        return $this;
    }

    public function createQuery($sql) {
        $this->sql = $sql;
        return $this;
    }

    public function selectDb($dbName) {
        mysql_select_db($dbName, $this->link);
        return $this;
    }

    public function selectCharset($dbName) {
        mysql_set_charset($dbName, $this->link);
        return $this;
    }

    public function getOneResult() {

        $result = mysql_query($this->sql, $this->link);
        while ($row = mysql_fetch_array($result)) {
            $allcount[] = $row;
        }
        return $allcount[0][0];
    }

    public function getResult() {

        $result = mysql_query($this->sql, $this->link);
        while ($row = mysql_fetch_assoc($result)) {
            $allcount[] = $row;
        }
        return isset($allcount)?$allcount:array();
    }
    
    public function getSQL() {

        return $this->sql;
    }
    
     public function hydrateByField($field){
        $result = mysql_query($this->sql, $this->link);
        
        while ($row = mysql_fetch_assoc($result)) {
            $allcount[] = $row[$field];
        }
        return $allcount;
        
        
    }
    
    public function hydrateByFieldAssoc($field, $assocField){
        
        $result = mysql_query($this->sql, $this->link);
        while ($row = mysql_fetch_assoc($result)) {
            $allcount[$row[$assocField]] = $row[$field];
        }
        return $allcount;
        
        
    }

    public function setLimit($startPosition, $count) {
        if (!stristr($this->sql, "LIMIT")) {
            $this->sql = $this->sql . " LIMIT " . $startPosition . ", " . $count;
            return $this;
        } else {
            $this->generateError('Too many LIMITs in SQL Query');
        }
    }

    public function __destruct() {
        mysql_close($this->link);
    }
    
    public function execute(){
        
         $result = mysql_query($this->sql, $this->link);
         
         if ($result) return true;
         else return false;
    }
    
    public function searchFields($fieldName){
        $structure = $this->getDbStrucure();
        
    }
    
    public function getDbStrucure(){
    
        $dbList = mysql_list_dbs();
        
        $dbListArray = array();
        
        while($db = mysql_fetch_object($dbList)){
            $dbName = $db->Database;

            $dbListArray[$dbName] = $dbName;
                $query = "SHOW TABLES FROM $dbName";
                $res = mysql_query($query);
                $tmpArray = array();
    //                
                    while($row = mysql_fetch_array($res)){
                        $resF = mysql_query("SHOW COLUMNS FROM ".$row['Tables_in_'.$dbName]);
                        
                        $fieldsTmpArray = array();
                        
                        if (mysql_num_rows($resF) > 0) {
                            
                            while ($rowT = mysql_fetch_assoc($resF)) {
                                if(count($rowT)){
                                    $fieldsTmpArray [$rowT['Field']]=$rowT;
                                }
                            }
                        }
                        $tmpArray[$row['Tables_in_'.$dbName]] = $fieldsTmpArray;
                    }
                $dbListArray[$dbName] = $tmpArray;
        }
        
        return $dbListArray;
        
    }

}

?>